import InterbankTransferBusiness from './biz';
import { AsyncStorage, } from 'react-native';


export default class InterbankTransferData {
  constructor(pageMode) {
    this.pageMode = pageMode;
    this.refreshBizeData();
    this.refreshPageListConfig();
  }
  refreshBizeData() {
    const result = {};
    if (this.pageMode == 0) {
      result.payerInfo = this.payerInfoData();
    }
    result.payeeInfo = this.payeeInfoData();
    this.bizData = result;
  }

    bizData = {
      /** 付款方信息 */
      payerInfo: {},
      /** 收款方信息 */
      payeeInfo: {},
    }

    payerInfoData() {
      return {
        accNo: '', /** 付款账户 string */
        currencyType: 0, /** 币种 int */
        accBalance: '', /** 账户余额， string */
        dealAmount: 0, /** 汇款金融 number double */
        chargeFee: '0.00', /** 手续费 string */
        remark: '', /** 汇款附言 string */
        useType: 1, /** 汇款用途 int */
        chargeType: 1, /** 费用承担方式 int */
      };
    }
    payeeInfoData() {
      return {
        receiverType: 1, /** 收款方类型 1 个人 2 企业 int */
        receiverName: '', /** 收款方姓名 string */
        receiverBankCard: '', /** 收款人账号 */
        receiverCountryCode: '', /** 收款人国家地区 string */
        receiverCountryName: '',
        receiverAddress: '', /** 收款人地址 string */
        receiverBankName: '', /** 收款人银行名称 string */
        receiverBankSwiftNo: '', /** 收款人银行 swfit号 */
        receiverBankAddress: '', /** 收款人银行地址 string */
        receiverBankCountryCode: '', /** 收款行国家地区 sting */
        receiverBankCountryName: '',
        mobileRemark: '', /** 收款人短信通知 */
        ifHasAgent: 0, /** 是否有代理行 int */
        chargeType: 0, /* 费用承担方式 */
        agentBankSwiftNo: '', /** 代理行swift号 */
        agentBankAddress: '', /** 代理行开户行地址 */
        agentBankCountryCode: '', /** 代理行银行国家 */
        agentBankCountryName: '',
        agentBankName: '', /** 代理行开户行名称 */
        mobile : '', /**手机号 */
      };
    }


  pageListConfig = {}

  refreshPageListConfig() {
    const result = [];
    if (this.pageMode == 0) {
      result.push(this.payerInfoPageConfig());
    }
    result.push(this.payeeInfoPageConfig());

    console.log(`pageListConfig is ${JSON.stringify(result)}`);
    this.pageListConfig = result;
  }

  payerInfoPageConfig() {
    return {
      title: '汇款方信息',
      bizDataKey: 'payerInfo',
      data: [
        {
          title: '汇款方信息',
          type: 'header',
        },
        {
          title: "付款账户",
          showArrow: false,
          placeholder: '',
          bindValue: 'accNo',
          editable: false,
        },
        {
          title: "币种",
          showArrow: true,
          bindValue: 'currencyType',
          type: 'selection',
        },
        {
          title: "账户余额",
          showArrow: false,
          bindValue: 'accBalance',
          editable: false,
        },
        {
          title: "汇款金额",
          showArrow: false,
          bindValue: 'dealAmount',
          onValueChaneds: (value, pageHandler) => {
            InterbankTransferBusiness.chargeFeeCal(value, (info) => {
              pageHandler.state.bizData.payerInfo.chargeFee = info.chargeFee;
              pageHandler.setState(pageHandler.state);
            });
          },
        },
        {
          title: '费用承担方式',
          showArrow: false,
          placeholder: '',
          bindValue: 'chargeFee',
          editable: false,
          displayFilter: (value) => {
            return `共同承担SHA 手续费${value}USD`;
          },
        },
        {
          title: '汇款附言',
          showArrow: false,
          placeholder: '请输入汇款附言（英文大写）',
          bindValue: 'remark',
        },
        {
          title: '汇款用途',
          placeholder: '请选择',
          showArrow: true,
          bindValue: 'useType',
          type: 'selection',
        },
      ],
    };
  }
  isEnterpriseReciver() {
    const recType = this.bizData.payeeInfo.receiverType;
    const result = (recType && parseInt(recType) == 2);
    return result;
  }
  payeeInfoPageConfig() {
    return {
      title: '收款方信息',
      bizDataKey: 'payeeInfo',
      data: [
        {
          title: '收款方信息',
          type: 'header',
        },
        {
          title: '收款方类型',
          showArrow: true,
          bindValue: 'receiverType',
          type: 'selection',
        },
        {
          title: this.isEnterpriseReciver() ? '收款企业姓名' : '收款人姓名',
          showArrow: false,
          tailIcon: this.pageMode == 0 ? 'user' : null,
          tailTapAction: (pageHandler) => {
            pageHandler.props.navigation.navigate('PayeeUserList', {
              receiverType: parseInt(this.bizData.payeeInfo.receiverType),
              callback: (payeeInfo) => {
                console.log('my payeeInfo is ', payeeInfo);
                pageHandler.state.bizData.payeeInfo = payeeInfo;
                pageHandler.setState(pageHandler.state);
              }, });
          },
          bindValue: 'receiverName',
        },
        {
          title: '收款手机号',
          bindValue:'mobile'
        },
        {
          title: this.isEnterpriseReciver() ? '收款企业账号' : '收款人账号',
          bindValue: 'receiverBankCard',
        },
        {
          title: this.isEnterpriseReciver() ? '收款企业国家/地区' : '收款人国家/地区',
          showArrow: true,
          bindValue: 'receiverCountryName',
          tapAction: async (pageHandler) => {
            await AsyncStorage.setItem('listStatus', "0");
            pageHandler.props.navigation.navigate('Country', { pageMode: '1',
            callback: (element) => {
              pageHandler.state.bizData.payeeInfo.receiverCountryCode = element.countryCode;
              pageHandler.state.bizData.payeeInfo.receiverCountryName = element.countryName;
              pageHandler.setState(pageHandler.state);
            }, });
          },
        },
        {
          title: this.isEnterpriseReciver() ? '收款企业地址' : '收款人地址',
          placeholder: '请输入字母（大写），数字及符号',
          bindValue: 'receiverAddress',
        },
        {
          title: '收款银行SWIFT码',
          placeholder: '请输入8或11位SWIFT码',
          bindValue: 'receiverBankSwiftNo',
        },
        {
          title: '收款银行国家',
          showArrow: true,
          bindValue: 'receiverBankCountryName',
          tapAction: async (pageHandler) => {
            await AsyncStorage.setItem('listStatus', "0");
            pageHandler.props.navigation.navigate('Country', { pageMode: '1',
              callback: (element) => {
                pageHandler.state.bizData.payeeInfo.receiverBankCountryCode = element.countryCode;
                pageHandler.state.bizData.payeeInfo.receiverBankCountryName = element.countryName;
                pageHandler.setState(pageHandler.state);
              }, });
          },
        },
        {
          title: '收款银行名称(英文)',
          placeholder: '请输入收款银行名称',
          bindValue: 'receiverBankName',
        },
        {
          title: '收款银行地址',
          placeholder: '请输入收款银行地址(英文大写)',
          bindValue: 'receiverBankAddress',
        },
        {
          title: '短信通知',
          placeholder: '可不填',
          bindValue: 'mobileRemark',
        },
        {
          type: 'tip',
          title: '资金从我行汇出成功后,将通知收款方',
        },
        {
          spaceHeight: 10,
          title: '收款方是和否有代理行？',
          showSwitch: true,
          value: () => {
            return this.bizData.payeeInfo.ifHasAgent == 1;
          },
          onSwitchChanged: (swithIsOn, pageHandler) => {
            pageHandler.state.bizData.payeeInfo.ifHasAgent = swithIsOn;
            pageHandler.setState(pageHandler.state);
          },
        },
        {
          title: '代理行SWIFT码',
          placeholder: '请输入8或11位SWIFT码',
          bindValue: 'agentBankSwiftNo',
          shouldShow: () => {
            return this.bizData.payeeInfo.ifHasAgent == 1;
          },
        },
        {
          title: '代理行国家',
          showArrow: true,
          bindValue: 'agentBankCountryName',
          shouldShow: () => {
            return this.bizData.payeeInfo.ifHasAgent == 1;
          },
          tapAction: async (pageHandler) => {
            await AsyncStorage.setItem('listStatus', "0");
            pageHandler.props.navigation.navigate('Country', { pageMode: '1',
              callback: (element) => {
                pageHandler.state.bizData.payeeInfo.agentBankCountryCode = element.countryCode;
                pageHandler.state.bizData.payeeInfo.agentBankCountryName = element.countryName;
                pageHandler.setState(pageHandler.state);
              }, });
          },
        },
        {
          title: '代理行开户行名称',
          placeholder: '请输入代理行名称',
          bindValue: 'agentBankName',
          shouldShow: () => {
            return this.bizData.payeeInfo.ifHasAgent == 1;
          },
        },
        {
          title: '代理行地址',
          placeholder: '请输入字母（大写）, 数字及符号',
          bindValue: 'agentBankAddress',
          shouldShow: () => {
            return this.bizData.payeeInfo.ifHasAgent == 1;
          },
        },
        {
          spaceHeight: 10,
          type: 'submit',
          title: this.pageMode == 1 ? '保存' : '下一步',
          onclick: () => {
            console.log('下一步 被点击');
          },
        },
      ],
    };
  }
}
